/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package portal.system.inzerat.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.management.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.JSONArray;
import sun.org.mozilla.javascript.internal.json.JsonParser;

/**
 *
 * @author Riso-Desktop
 */
@WebServlet(name = "AutoComplete", urlPatterns = {"/AutoCompleteName"})
public class AutoCompleteName extends HttpServlet {

    String driver = portal.constants.Database.DATABASE_DRIVER;
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    String userName = portal.constants.Database.DATABASE_USER;
    String password = portal.constants.Database.DATABASE_PASSWORD;
    String URL = portal.constants.Database.DATABASE_URL;
    HttpSession session;
    PrintWriter out;

    @Override
    public void init() {
        try {
            super.init();
            Class.forName(driver);
            con = DriverManager.getConnection(URL, userName, password);
        } catch (Exception ex) {
        }
    }
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        out = response.getWriter();
        try {
            
        response.setHeader("Cache-control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "-1");
        stmt = con.createStatement();
        String query = request.getParameter("term");
        query = query.toLowerCase();
        String pom = query;
        ArrayList slova = new ArrayList();
        while(pom.length()>0){
            if(pom.indexOf(" ")>0){
            slova.add(pom.substring(0,pom.indexOf(" ")));
            pom = pom.substring(pom.indexOf(" ")+1);
            }else {
                slova.add(pom);
                pom="";
            }
        }
        StringBuilder sb = new StringBuilder();
        for(int i =0;i<slova.size();i++){
            sb.append(" +"+slova.get(i));
        }
        sb.append("*");
        query = sb.toString();
        String sql= "select hladany_vyraz from hladane_vyrazy where match(hladany_vyraz) against('"+query+"' IN BOOLEAN MODE) limit 0,10";
        //String sql = "select hladany_vyraz from hladane_vyrazy where hladany_vyraz like '"+query+"%'";
        System.out.println(sql);
        rs = stmt.executeQuery(sql);
        JSONArray arrayObj=new JSONArray();
        //System.out.println(query);
        
        while(rs.next()){
            arrayObj.put(rs.getString("hladany_vyraz"));
        }
        
        out.println(arrayObj.toString());
        
        } catch (Exception e) {
            e.printStackTrace();
        }
        out.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
